﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var keyValue = $.request("keyValue");
    var sites = {};
    var services = [];
    var units = [];

    $(function () {

        $("#F_OrderId").val(keyValue);

        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetTakelookGridByIntentionSiteJson",
            data: { orderId: keyValue },
            dataType: "json",
            async: false,
            success: function (siteData) {
                //console.log(siteData);
                $.ajax({
                    url: "/OrderFlowManage/ClientOrder/GetItemJson?enCode=fylm",//ServicesCost",
                    data: {},
                    dataType: "json",
                    async: false,
                    success: function (serviceData) {
                        $.ajax({
                            url: "/OrderFlowManage/ClientOrder/GetItemJson?enCode=units",
                            data: {},
                            dataType: "json",
                            async: false,
                            success: function (unitsData) {
                                sites = siteData;
                                services = serviceData;
                                units = unitsData;
                                var sitehtml = bind_sitedata();
                                var servicehtml = bind_servicedata();
                                var unitshtml = bind_unitsdata();
                                $("#F_SiteId0").html(sitehtml).bindSelect();
                                $("#F_ServiceCode0").html(servicehtml).bindSelect();
                                $("#F_PriceUnit0").html(unitshtml).bindSelect();

                                $("#btnAdd").prop("disabled", false);
                            }
                        });
                    }
                });

                
            }
        });

    });

    function bind_sitedata() {
        var html = '';
        if (sites && sites.rows) {
            for (var i in sites.rows) {
                var item = sites.rows[i];
                //html += '<option value="' + item.F_SiteId + '|' + item.F_SpaceId + '">';
                html += '<option value="' + i + '">';
                html += item.SiteName;
                html += '</option>';
            }
            return html;
        }
    }

    function bind_servicedata() {
        var html = '';
        if (services && services.length) {
            for (var i = 0; i < services.length; i++) {
                var item = services[i];
                // 场地费为 1，在配套服务中，不需要
                if (item.Value != 1) {
                    html += '<option value="' + item.Value + '">';
                    html += item.Text;
                    html += '</option>';
                }
            }
            return html;
        }
    }

    function bind_unitsdata() {
        var html = '';
        if (units && units.length) {
            for (var i = 0; i < units.length; i++) {
                var item = units[i];
                html += '<option value="' + item.Value + '">';
                html += item.Text;
                html += '</option>';
            }
            return html;
        }
    }

    function btn_add() {
        var index = parseInt($("#bodyIntention").attr("index"));
        index++;
        
        var html = '';
        html += '<tr>';
        html += '<td>';
        html += '<select id="F_SiteId' + index + '" name="F_SiteId' + index + '" type="text" class="form-control required">';
        html += bind_sitedata();
        html += '</select>';
        html += '</td>';
        html += '<td>';
        html += '<select id="F_ServiceCode' + index + '" name="F_ServiceCode' + index + '" type="text" class="form-control required">';
        html += bind_servicedata();
        html += '</select>';
        html += '</td>';
        html += '<td class="formValue">';
        html += '<input id="F_Num' + index + '" name="F_Num' + index + '" type="text" class="form-control required" />';
        html += '</td>';
        html += '<td class="formValue">';
        html += '<select id="F_PriceUnit' + index + '" name="F_PriceUnit' + index + '" type="text" class="form-control required">';
        html += bind_unitsdata();
        html += '</select>';
        html += '</td>';
        html += '<td class="formValue">';
        html += '<textarea id="F_Remark' + index + '" name="F_Remark' + index + '" rows="1" class="form-control"></textarea>';
        html += '</td>';
        html += '</tr>';

        $("#bodyIntention").append(html);
        $("#F_SiteId" + index).bindSelect();
        $("#F_ServiceCode" + index).bindSelect();
        $("#F_PriceUnit" + index).bindSelect();

        $("#bodyIntention").attr("index", index);
    }

    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        var index = parseInt($("#bodyIntention").attr("index")),
            orderId = $("#F_OrderId").val(),
            param = {
                Data: []
            };
        for (var i = 0; i <= index; i++) {
            param.Data.push({
                F_OrderId: orderId,
                F_SiteAndSpaceId: $("#F_SiteId" + i).val(),
                F_ServiceCode: $("#F_ServiceCode" + i).val(),
                F_ServiceName: $("#F_ServiceCode" + i+" option:selected").text(),
                F_Num: $("#F_Num" + i).val(),
                F_PriceUnit: $("#F_PriceUnit" + i).val(),
                F_Remark: $("#F_Remark" + i).val()
            });
        }
        console.log(param);
        $.submitForm({
            url: "/OrderFlowManage/ClientOrder/SubmitIntentionServiceForm?keyValue=" + keyValue,
            param: param,
            success: function () {
                $.currentWindow().vm.intentionsite.initIntentionService();
                $.loading(false);
            }
        });
    }
</script>

<form id="form1">
    <div style="margin: 20px;">
        <div class="text-right">
            <button id="btnAdd" type="button" class="btn btn-info btn-xs" onclick="btn_add()" disabled><span class="glyphicon glyphicon-plus"></span> 添加</button>
        </div>
        <table class="form">
            <thead>
                <tr>
                    <th>意向场地</th>
                    <th>配套服务</th>
                    <th>数量</th>
                    <td>价格单位</td>
                    <th>服务描述</th>
                </tr>
            </thead>
            <tbody id="bodyIntention" index="0">
                <tr>
                    <td>
                        <input type="hidden" id="F_OrderId" name="F_OrderId" value="" />
                        <select id="F_SiteId0" name="F_SiteId0" type="text" class="form-control required"></select>
                    </td>
                    <td>
                        <select id="F_ServiceCode0" name="F_ServiceCode0" type="text" class="form-control required"></select>
                    </td>
                    <td class="formValue">
                        <input id="F_Num0" name="F_Num0" type="text" class="form-control required" />
                    </td>
                    <td class="formValue">
                        <select id="F_PriceUnit0" name="F_PriceUnit0" type="text" class="form-control"></select>
                    </td>
                    <td class="formValue">
                        <textarea id="F_Remark0" name="F_Remark0" rows="1" class="form-control"></textarea>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</form>


